#include <bits/stdc++.h>
using namespace std;
const int N = 55;
string s[N];
int diff(string &a, string &b) {
  int ret = 0;
  for (int i = 0; i < a.size(); ++i) ret += abs(a[i] - b[i]);
  return ret;
}
int main() {
  int tc;
  cin >> tc;
  while (tc--) {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) cin >> s[i];
    int ans = 0x3f3f3f3f;
    for (int i = 1; i < n; ++i)
      for (int j = 0; j < i; ++j) {
        ans = min(ans, diff(s[i], s[j]));
      }
    cout << ans << endl;
  }
}
